package cst.a324yyf.stratproject324;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cst.a324yyf.stratproject324.bean.WeatherInfo;
import cst.a324yyf.stratproject324.listener.WeatherListener;
import cst.a324yyf.stratproject324.model.Weather51Model;
import cst.a324yyf.stratproject324.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener {
    private EditText cityid;
    private TextView city,temp;
    private Button button;
    private TextView wd;
    private TextView ws;

    void init(){
        cityid=(EditText)findViewById(R.id.cityid);
        city=(TextView)findViewById(R.id.city);
        temp=(TextView)findViewById(R.id.temp);
        button=(Button)findViewById(R.id.button);
        wd = (TextView) findViewById(R.id.tv_WD);
        ws = (TextView) findViewById(R.id.tv_WS);
        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                WeatherModel weathermodel=new WeatherModel();
                weathermodel.getWeather(cityid.getText().toString(),this);
                break;
            case R.id.button2:
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(cityid.getText().toString(), this);
                break;
            default:
                break;

        }

    }
    @Override
    public void onResponse(WeatherInfo weather) {

        if (weather != null) {
            city.setText(weather.getCity());
            city.setText(weather.getCityid());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());

        } else {
            city.setText("未知");
        }

    }

    //获取数据失败
    @Override
    public void onFail(String msg) {
        //取消数据加载进度条

        city.setText(msg);
        //Toast.makeText(this,"fail:"+msg,Toast.LENGTH_SHORT).show();
    }
}

